<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    style="max-width: 700px; min-width: 500px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="标题">
        <span>{{ formData.title }}</span>
        <!-- <el-input v-model="formData.title" placeholder="请输入标题" /> -->
      </el-form-item>
      <!--      <el-form-item label="消息类型">-->
      <!--        <span>{{ formData.msgTypeLable }}</span>-->
      <!--        &lt;!&ndash; <el-input v-model="formData.msgType" placeholder="请输入消息类型" /> &ndash;&gt;-->
      <!--      </el-form-item>-->
      <el-form-item label="消息内容">
        <span>{{ formData.msgBody }}</span>
        <!-- <el-input v-model="formData.msgBody" placeholder="请输入消息内容" /> -->
      </el-form-item>
      <el-form-item label="发送人姓名">
        <span>{{ formData.createdUserName }}</span>
        <!-- <el-input v-model="formData.sendUserId" placeholder="请输入发送人id" /> -->
      </el-form-item>
      <el-form-item label="发送时间">
        <span>{{ formData.sendTime }}</span>
        <!-- <el-input v-model="formData.sendTime" placeholder="请输入发送时间" /> -->
      </el-form-item>

      <el-form-item label="备注">
        <span>{{ formData.remarks }}</span>
        <!-- <el-input v-model="formData.remarks" placeholder="请输入备注" /> -->
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";

defineOptions({
  name: "SYSMESSAGEADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  title: [{ required: true, message: "请输入标题", trigger: "blur" }],
  url: [{ required: true, message: "请输入url", trigger: "blur" }],
  msgType: [{ required: true, message: "请输入消息类型", trigger: "blur" }],
  msgBody: [{ required: true, message: "请输入消息内容", trigger: "blur" }],
  sendUserId: [{ required: true, message: "请输入发送人id", trigger: "blur" }],
  sendTime: [{ required: true, message: "请输入发送时间", trigger: "blur" }],
  recevieUserId: [
    { required: true, message: "请输入接收人id", trigger: "blur" },
  ],
  readTime: [{ required: true, message: "请输入阅读时间", trigger: "blur" }],
  readed: [
    { required: true, message: "请输入是否已读 0未读 1 已读", trigger: "blur" },
  ],
  remarks: [{ required: true, message: "请输入备注", trigger: "blur" }],
  deleted: [
    {
      required: true,
      message: "请输入是否已被删除（0 - 未删除， 1 - 已删除）",
      trigger: "blur",
    },
  ],
  enabled: [
    {
      required: true,
      message: "请输入是否已被禁用（0 - 禁用， 1 - 启用）",
      trigger: "blur",
    },
  ],
  createdUserName: [
    { required: true, message: "请输入创建人姓名", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { required: true, message: "请输入最近一次更新人姓名", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  title: "",
  url: "",
  msgTypeLable: "",
  msgBody: "",
  sendUserId: "",
  sendTime: "",
  recevieUserId: "",
  readTime: "",
  readed: "",
  remarks: "",
  deleted: "",
  enabled: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  sendUserName: " ",
});

watch(
  () => props.data,
  (newVal: any) => {
    if (newVal?.id) {
      const {
        id,
        version,
        title,
        url,
        msgTypeLable,
        msgBody,
        sendUserId,
        sendTime,
        recevieUserId,
        readTime,
        readed,
        remarks,
        deleted,
        enabled,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        sendUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        title,
        url,
        msgTypeLable,
        msgBody,
        sendUserId,
        sendTime,
        recevieUserId,
        readTime,
        readed,
        remarks,
        deleted,
        enabled,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        sendUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.title = "";
  formData.url = "";
  formData.msgTypeLable = "";
  formData.msgBody = "";
  formData.sendUserId = "";
  formData.sendTime = "";
  formData.recevieUserId = "";
  formData.readTime = "";
  formData.readed = "";
  formData.remarks = "";
  formData.deleted = "";
  formData.enabled = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.sendUserName = "";
}

const loading = ref(false);

/** 保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/system/sysmessage/save",
        method: "post",
        data: formData.id
          ? { ...formData, readed: 1, readTime: new Date().toISOString() }
          : formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table");
    }
  });
}

/** 查询详细表 */
async function getDatails() {
  const data = await request({
    url: "/system/sysmessage/getDetails",
    method: "post",
    data: formData,
  });
}

onMounted(() => {
  if (formData.id) {
    getDatails();
  }
});

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
